package test;

import B2.ControlPanel;
import B2.Event;
import B2.State;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.PrintWriter;

public class B2test {
    @Test
        //由于使用状态图时操作栏会被设定为read-only所以只能是定义式地测试代码
        //这里为输入错误三次后进入锁定状态
    void have_morethan_three_times_wrong_pwd(){
        ControlPanel controlPanel = new ControlPanel();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.fire(Event.toComparing);
        controlPanel.setPwd_is_correct(false); //第一次输入密码错误
        controlPanel.add_a_wa_cnt();
        controlPanel.fire(Event.toSelecting);
        controlPanel.fire(Event.toLocked);
        controlPanel.fire(Event.toReading);

        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.fire(Event.toComparing);
        controlPanel.setPwd_is_correct(false); //第二次输入密码错误
        controlPanel.add_a_wa_cnt();
        controlPanel.fire(Event.toSelecting);
        controlPanel.fire(Event.toLocked);
        controlPanel.fire(Event.toReading);

        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.fire(Event.toComparing);
        controlPanel.setPwd_is_correct(false); //第三次输入密码错误
        controlPanel.add_a_wa_cnt();
        controlPanel.fire(Event.toSelecting);
        controlPanel.fire(Event.toLocked);
        State state = controlPanel.getCurrentState();
        Assertions.assertThat(state).isEqualTo(State.Locked);
        controlPanel.fire(Event.toReading);
        state = controlPanel.getCurrentState();
        Assertions.assertThat(state).isEqualTo(State.Reading);
    }
    @Test
    void have_lessthan_three_times_wrong_pwd_and_password_is_correct(){
        ControlPanel controlPanel = new ControlPanel();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.fire(Event.toComparing);
        controlPanel.setPwd_is_correct(false); //第一次输入密码错误
        controlPanel.add_a_wa_cnt();
        controlPanel.fire(Event.toSelecting);
        controlPanel.fire(Event.toLocked);
        controlPanel.fire(Event.toReading);

        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.add_a_character();
        controlPanel.fire(Event.toComparing);
        controlPanel.setPwd_is_correct(true); //第二次输入密码正确
        controlPanel.fire(Event.toSelecting);
        State state = controlPanel.getCurrentState();
        Assertions.assertThat(state).isEqualTo(State.Selecting);
        controlPanel.fire(Event.toLocked);
        controlPanel.fire(Event.toReading);
        state = controlPanel.getCurrentState();
        Assertions.assertThat(state).isEqualTo(State.Reading);
    }
}
